package com.example.yin.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.yin.constant.Constants;
import com.example.yin.domain.Collect;
import com.example.yin.domain.Consumer;
import com.example.yin.domain.ConsumerSongList;
import com.example.yin.domain.SongList;
import com.example.yin.service.impl.CollectServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;

@RestController
@Controller
public class CollectController {
/*用户收藏的歌单*/

    @Autowired
    private CollectServiceImpl collectService;

    @Configuration
    public class MyPicConfig implements WebMvcConfigurer {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            String os = System.getProperty("os.name");
            if (os.toLowerCase().startsWith("win")) { // windos系统
                registry.addResourceHandler("/img/songListPic/**")
                        .addResourceLocations("file:" + Constants.RESOURCE_WIN_PATH + "\\img\\songListPic\\");
            } else { // MAC、Linux系统
                registry.addResourceHandler("/img/songListPic/**")
                        .addResourceLocations("file:" + Constants.RESOURCE_MAC_PATH + "/img/songListPic/");
            }
        }
    }

//    添加收藏的歌曲
    @ResponseBody
    @RequestMapping(value = "/collection/add", method = RequestMethod.POST)
    public Object addCollection(HttpServletRequest req){

        JSONObject jsonObject = new JSONObject();
        String user_id = req.getParameter("userId");
        String song_id=req.getParameter("songId");
        String song_list_id=req.getParameter("songListId");
        if (song_id == "" || song_id == null || song_id.equals("null")){
            jsonObject.put("code", -2);
            jsonObject.put("msg", "请选择歌曲");
            return jsonObject;
        }
        Collect collect = new Collect();
        collect.setUserId(Integer.parseInt(user_id));
        collect.setSongId(Integer.parseInt(song_id));
        collect.setSongListId(Integer.parseInt(song_list_id));
        collect.setCreateTime(new Date());
//        System.out.println("----------------"+user_id+"----------------"+song_id+"----------------"+song_list_id);
        int res = collectService.addSongToList(collect);
        if (res > 0){
            jsonObject.put("code", 1);
            jsonObject.put("msg", "收藏成功");
            return jsonObject;
        } else if(res == -1){
            jsonObject.put("code", -1);
            jsonObject.put("msg", "歌单已收藏该歌曲");
            return jsonObject;
        } else{
            jsonObject.put("code", 0);
            jsonObject.put("msg", "收藏失败");
            return jsonObject;
        }
    }

//    返回所有用户收藏列表
//    @RequestMapping(value = "/collection", method = RequestMethod.GET)
//    public Object allCollection(){
//        return collectService.allCollect();
//    }

//    返回的指定用户ID的用户歌单
    @RequestMapping(value = "/collection/detail", method = RequestMethod.GET)
    public Object collectionOfUser(HttpServletRequest req){
        String userId = req.getParameter("userId");
        return collectService.allLists(Integer.parseInt(userId));
    }

//    返回的指定用户指定歌单的用户歌单的里歌曲
    @RequestMapping(value = "/consumerSongList/detail", method = RequestMethod.GET)
    public Object getAllSongsOfUserByListId(HttpServletRequest req){
        String songListId = req.getParameter("songListId");
        return collectService.allSongs(Integer.parseInt(songListId));
    }

//    取消收藏的歌曲
    @ResponseBody
    @RequestMapping(value = "/collection/delete", method = RequestMethod.POST)
    public Object deleteCollection(HttpServletRequest req){
        JSONObject jsonObject = new JSONObject();
        String user_id = req.getParameter("userId").trim();
        String song_id = req.getParameter("songId").trim();
        String song_list_id = req.getParameter("songListId").trim();
//        System.out.println("-----------------"+user_id+"----------------"+song_id+"------------"+song_list_id+"----------");
        int res = collectService.deleteBySongId(Integer.parseInt(user_id), Integer.parseInt(song_id),Integer.parseInt(song_list_id));
        if (res > 0){
            jsonObject.put("code", 1);
            jsonObject.put("msg", "取消成功");
            return jsonObject;
        } else {
            jsonObject.put("code", 0);
            jsonObject.put("msg", "取消失败");
            return jsonObject;
        }
    }

//    更新歌单信息
    @ResponseBody
    @RequestMapping(value = "/consumerSongList/update", method = RequestMethod.POST)
    public Object updateCollectMsg(HttpServletRequest req){
        JSONObject jsonObject = new JSONObject();
        String id = req.getParameter("id").trim();
        String title = req.getParameter("title").trim();
        String introduction = req.getParameter("introduction").trim();
        String style = req.getParameter("style").trim();

        ConsumerSongList consumerSongList = new ConsumerSongList();
        consumerSongList.setId(Integer.parseInt(id));
        consumerSongList.setTitle(title);
        consumerSongList.setIntroduction(introduction);
        consumerSongList.setStyle(style);
        boolean res = collectService.updateSongList(consumerSongList);
        if (res){
            jsonObject.put("code", 1);
            jsonObject.put("msg", "修改成功");
            return jsonObject;
        }else {
            jsonObject.put("code", 0);
            jsonObject.put("msg", "修改失败");
            return jsonObject;
        }
    }

    //    更新歌单图片
    @ResponseBody
    @RequestMapping(value = "/consumerSongList/pic/update", method = RequestMethod.POST)
    public Object updateSongListPic(@RequestParam("file") MultipartFile avatorFile, @RequestParam("id")int id){
        JSONObject jsonObject = new JSONObject();

        if (avatorFile.isEmpty()) {
            jsonObject.put("code", 0);
            jsonObject.put("msg", "文件上传失败！");
            return jsonObject;
        }
        String fileName = System.currentTimeMillis()+avatorFile.getOriginalFilename();
        String filePath = System.getProperty("user.dir") + System.getProperty("file.separator") + "music-server\\img\\songListPic" ;
        File file1 = new File(filePath);
        if (!file1.exists()){
            file1.mkdir();
        }

        File dest = new File(filePath + System.getProperty("file.separator") + fileName);
        String storeAvatorPath = "/img/songListPic/"+fileName;
        try {
            avatorFile.transferTo(dest);
            ConsumerSongList songList = new ConsumerSongList();
            songList.setId(id);
            songList.setPic(storeAvatorPath);
//            System.out.println("----------------------------------------------------");
//            System.out.println(dest);
            boolean res = collectService.updateSongListAvator(songList);
            if (res){
                jsonObject.put("code", 1);
                jsonObject.put("avator", storeAvatorPath);
                jsonObject.put("msg", "上传成功");
                return jsonObject;
            }else {
                jsonObject.put("code", 0);
                jsonObject.put("msg", "上传失败");
                return jsonObject;
            }
        }catch (IOException e){
            jsonObject.put("code", 0);
            jsonObject.put("msg", "上传失败" + e.getMessage());
            return jsonObject;
        }finally {
            return jsonObject;
        }
    }

    //新建歌单
    @ResponseBody
    @RequestMapping(value = "/consumerSongList/add", method = RequestMethod.POST)
    public Object addConsumerSongList(HttpServletRequest req){
        JSONObject jsonObject = new JSONObject();
        String userId = req.getParameter("user_id").trim();
        String title = req.getParameter("title").trim();
        String pic = req.getParameter("pic").trim();
        String introduction = req.getParameter("introduction").trim();
        String style = req.getParameter("style").trim();

        ConsumerSongList consumerSongList = new ConsumerSongList();
        consumerSongList.setTitle(title);
        consumerSongList.setPic(pic);
        consumerSongList.setIntroduction(introduction);
        consumerSongList.setStyle(style);
        consumerSongList.setUserId(Integer.parseInt(userId));
        boolean res = collectService.addSongList(consumerSongList);
        if (res){
            jsonObject.put("code", 1);
            jsonObject.put("msg", "新建成功");
            return jsonObject;
        }else {
            jsonObject.put("code", 0);
            jsonObject.put("msg", "新建失败");
            return jsonObject;
        }
    }

    //搜索个人歌单
    @RequestMapping(value = "/collect/song/name/detail", method = RequestMethod.GET)
    public Object queryConsumerSongList(HttpServletRequest req){
        String name = req.getParameter("name").trim();
        String userId = req.getParameter("userId").trim();
        return collectService.queryConsumerSongList(name, Integer.parseInt(userId));
    }

    // 搜索个人歌单里的歌曲
    @RequestMapping(value = "/collect/songList/name/detail", method = RequestMethod.GET)
    public Object querySongInList(HttpServletRequest req){
        String name = req.getParameter("name").trim();
        String userId = req.getParameter("userId").trim();
        return collectService.querySongInList(name, Integer.parseInt(userId));
    }

    @ResponseBody
    @RequestMapping(value = "/collection/songList/delete", method = RequestMethod.POST)
    public Object deleteSongList(HttpServletRequest req){
        JSONObject jsonObject = new JSONObject();
        String song_list_id = req.getParameter("songListId").trim();
        boolean res = collectService.deleteSongList(Integer.parseInt(song_list_id));
        if (res){
            jsonObject.put("code", 1);
            jsonObject.put("msg", "删除成功");
            return jsonObject;
        } else {
            jsonObject.put("code", 0);
            jsonObject.put("msg", "删除失败");
            return jsonObject;
        }
    }

}

